import { takeEvery } from "redux-saga/effects";
import { NATIVE_CLASS } from "@common/constants";
import { callNativeFunc } from "@common/saga-helper";
import {
  SAGA_ORDER_MOVE_FINISH_OPS,
  SAGA_ORDER_MOVE_PUSH_2_SERVER,
  SAGA_ORDRE_MOVE_LOAD_OPS_FORM,
} from "./saga-types";
import { SET_ORDER_MOVE_OPS_FORM } from "./action-types";

function* loadOrderMoveOpsFormSaga() {
  yield takeEvery(SAGA_ORDRE_MOVE_LOAD_OPS_FORM, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.OrderMoveOpsHandler,
            action: "loadOpsFormData",
            needNonce: false,
            params: action.payload,
          },
          respActions: [SET_ORDER_MOVE_OPS_FORM],
        },
      ],
    });
  });
}

function* finishOrderMoveOpsSaga() {
  yield takeEvery(SAGA_ORDER_MOVE_FINISH_OPS, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.OrderMoveOpsHandler,
            action: "finish",
            needNonce: false,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

function* pushOrderMove2ServerSaga() {
  yield takeEvery(SAGA_ORDER_MOVE_PUSH_2_SERVER, function* (action) {
    yield* callNativeFunc({
      effects: [
        {
          payload: {
            classFullName: NATIVE_CLASS.OrderMoveHandler,
            action: "push2Server",
            needNonce: true,
            params: action.payload,
          },
          callback: action.payload.callback,
        },
      ],
    });
  });
}

export default [
  loadOrderMoveOpsFormSaga(),
  finishOrderMoveOpsSaga(),
  pushOrderMove2ServerSaga(),
];
